Public Class Variable
    Inherits WebControls.WebControl

    Private m_sName As String
    Private m_sValue As String
    Private m_iDataType As DataType = DataType.Default

    Public Enum DataType
        [Default]
        [String]
    End Enum

    Public Property Name() As String
        Get
            Return m_sName
        End Get
        Set(ByVal value As String)
            m_sName = value
        End Set
    End Property

    Public Property Value() As String
        Get
            Return m_sValue
        End Get
        Set(ByVal value As String)
            m_sValue = value
        End Set
    End Property

    Public Property Type() As DataType
        Get
            Return m_iDataType
        End Get
        Set(ByVal value As DataType)
            m_iDataType = value
        End Set
    End Property

    Private Function CurrentNamespace(ByVal Control As Control) As String
        If Control IsNot Nothing Then
            If TypeOf Control Is IJObjective Then
                With DirectCast(Control, IJObjective)
                    If String.IsNullOrEmpty(.ObjectName) Then
                        Return CurrentNamespace(Control.Parent)
                    Else
                        Return .Namespace
                    End If
                End With
            ElseIf TypeOf Control Is ScriptWriter Then
                Return ScriptWriter.DefaultNamespace
            Else
                Return CurrentNamespace(Control.Parent)
            End If
        Else
            Throw New Exception("Variables require at least one ancestral ClientEnhancedControl.")
        End If
    End Function

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        'Do Nothing
    End Sub

    Private Sub Variable_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Dim sbVariable As New Text.StringBuilder

        With sbVariable
            .Append(CurrentNamespace(Me.Parent))
            .Append(".")
            .Append(Name)
            .Append("=")

            If m_iDataType = DataType.Default Then
                .Append(Value)
            ElseIf m_iDataType = DataType.String Then
                .Append("'")
                .Append(Value)
                .Append("'")
            End If
            .AppendLine(";")
        End With

        ID = Common.GetUniqueID(Context)

        ScriptManager.RegisterStartupScript(Me, Me.GetType, ID, sbVariable.ToString(), True)
    End Sub
End Class